package com.xu.junit;

import com.xu.common.example.model.User;
import com.xu.common.example.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.Assert.assertEquals;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring/spring-context.xml", "classpath*:/spring/spring-datasource.xml", "classpath*:/spring/spring-mvc.xml" })
@Transactional
@WebAppConfiguration
public class JUnitStudy7_SpringJUnit4ClassRunner {

	@Autowired
	private UserService service = null;

	@Test
	public void test() {
		assertEquals(1, service.get(1).getId().intValue());
	}


	@Test
	@Rollback(true) //true 执行结束回滚. false不回滚
	public void testRollback() {
		int testTargetId = 1;
		String username = "zhangsan";
		
		User user = service.get(testTargetId);
		user.setUsername(username);
		assertEquals(1, service.edit(user).intValue());
		user = service.get(testTargetId);
		assertEquals(username, user.getUsername());
	}
}